<template>
  <qx-table :dbRowClick="rowDblClick" :option="tableOption" :remoteData="remoteData">
    <template v-slot:top>
      <qx-form v-model="searchForm" :option="tableOption" isSearch @submit="startSearch"></qx-form>
    </template>
    <template v-slot:menuLeftR>
      <q-icon color="warning" name="mdi-map-marker-question cursor-pointer cursor-hover" size="1.5rem">
        <q-tooltip class="q-pa-md" v-bind="$tooltipAttr">{{ $t('Misc.DblClickTips') }}</q-tooltip>
      </q-icon>
    </template>
  </qx-table>
  <q-modal v-model="modalVisible" :header="`${$t('Misc.SpecifyDetails')}`" :menuBtn="false" :persistent="false"
           width="620">
    <QInfo :columns="tableOption.column || []" :rowInfo="formInfo" cols="2"></QInfo>
  </q-modal>
</template>
<script setup>
import { i18n, toI18nName } from 'boot/i18n.js'
import { date } from 'quasar'
import { getRemoteData } from 'src/enum/ajaxRemoteData.js'
import { notificationStatus } from 'src/views/TaskCore/enum.js'
import { computed, onMounted, ref } from 'vue'

const currentDate = new Date()
const formInfo = ref({})
const AGVBrands = ref([])
const searchForm = ref({})
const modalVisible = ref(false)
const remoteData = ref({ dicUrl: '/tpm/yfagv/api/agvWarnHis/list' })
getRemoteData('agv_brand', '/yfagv/api/agvCfgBrand/list').then(data => (AGVBrands.value = data))
const tableOption = computed(() => {
  const defaultDate = date.formatDate(currentDate, 'YYYY-MM-DD')
  return {
    span: 22, export: true, labelWidth: 100, headerWidth: 60, searchSpan: 4, searchLabelWidth: 60,
    column: [{
      label: `${i18n.t('AGVCore.AGVBrand')}`, field: 'agvBrandCode', type: 'select', options: AGVBrands.value,
      minWidth: 80, props: { label: `${toI18nName('agvBrandNameZhCn')}`, value: 'agvBrandCode' }
    }, {
      label: `${i18n.t('AGVCore.AGVCode')}`, field: 'agvCode', minWidth: 80
    }, {
      label: `${i18n.t('Status')}`, field: 'agvRunStatus', minWidth: 100, search: false, hide: true
    }, {
      label: `${i18n.t('Misc.CollectionTimestamp')}`, field: 'collectDateStr', minWidth: 100, search: false, hide: true
    }, {
      label: `${i18n.t('CreationTime')}`, field: 'createdTime', type: 'daterange', minWidth: 90,
      searchValue: { from: defaultDate, to: defaultDate }, clearable: false
    }, {
      label: `${i18n.t('MapCore.LocationMap')}`, field: 'mapGroundCode', minWidth: 100, search: false
    }, {
      label: `${i18n.t('AGVCore.OriginalAlarmCode')}`, field: 'originalWarnCode', minWidth: 100, search: false,
      hide: true
    }, {
      label: `${i18n.t('AGVCore.OriginalAlarmDescription')}`, field: 'originalWarnDesc', minWidth: 100
    }, {
      label: `${i18n.t('Misc.VersionNo')}`, field: 'revision', minWidth: 100, search: false, hide: true
    }, {
      label: `${i18n.t('TaskCore.RelatedTaskCode')}`, field: 'taskCode', minWidth: 100, search: false
    }, {
      label: `${i18n.t('Misc.LastUpdateTime')}`, field: 'updatedTime', minWidth: 100, search: false, hide: true
    }, {
      label: `${i18n.t('AGVCore.StartAlarmPower')}`, field: 'warnStartElec', minWidth: 100, search: false,
      format: val => `${val || 0}%`
    }, {
      label: `${i18n.t('AGVCore.StartAlarmCoord')}`, field: 'warnStartPoint', minWidth: 100, search: false,
      format: val => `${(val || '').replace(',', ',\n')}`
    }, {
      label: `${i18n.t('AGVCore.EndAlarmPower')}`, field: 'warnEndElec', minWidth: 100, search: false,
      format: val => `${val || 0}%`
    }, {
      label: `${i18n.t('AGVCore.EndAlarmCoord')}`, field: 'warnEndPoint', minWidth: 100, search: false,
      format: val => `${(val || '').replace(',', ',\n')}`
    }, {
      label: `${i18n.t('Misc.ElapsedRunningMileage')}`, field: 'warnSumAllDistance', minWidth: 100, search: false
    }, {
      label: `${i18n.t('Misc.ElapsedRunningDuration')}`, field: 'warnSunAllDuration', minWidth: 100, search: false
    }, {
      label: `${i18n.t('Misc.TPMProcessingStatus')}`, field: 'warnTpmStatus',
      options: notificationStatus.value, useTag: true, minWidth: 100, search: false
    }]
  }
})

const rowDblClick = row => {
  formInfo.value = row
  modalVisible.value = true
}
const formatSearchForm = () => {
  const params = { ...searchForm.value }
  if (searchForm.value.createdTime?.from) {
    params.createdStartTime = params.createdTime?.from
    params.createdEndTime = params.createdTime?.to
    delete params.createdTime
  }
  return { ...params }
}
const startSearch = (_, done) => {
  remoteData.value.params = formatSearchForm()
  remoteData.value.clearSelected = true
  if (done) done()
}
onMounted(startSearch)
</script>